<?php
namespace App\Http\Controllers\v1;

use Illuminate\Http\Request;
use Mockery\Exception;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;
use App\Http\Controllers\RequestBackController;
use App\Http\Controllers\Controller;
use App\Http\Controllers\CheckController;
/*
   数据中心接口
*/
class DataCenterController extends Controller{
    /*
        资金流水显示
    */
    public function ShowTracking($request){
        if(!CheckController::CheckUser($request)){
            return RequestBackController::Structure(205);
        }
        $pos=$request->all();
		$page = isset($pos['page']) ? $pos['page']  : 1;
        $pageSize = isset($pos['pageSize']) ? $pos['pageSize'] : 10;
        $offset = ($page - 1) * $pageSize;
        if(CheckController::CheckParameter($pos,['UserId'])){
            $Purview = new PurviewController();
            $UserPurview = $Purview->PurviewVerdict($request,$pos['UserId']);
            //print_r($UserPurview);
            if($UserPurview == 'All'){//超级管理员显示所有流水
                $allMoney = DB::connection('mysql_other')->table('yl_proceeds as p')->select('p.proceeds_id as ProceedsId','p.proceeds_count as ProceedsCount','p.proceeds_creater_id as ProceedsCreaterId','p.proceeds_created_time as ProceedsCreatedTime','p.case_id as CaseId','p.proceeds_results_id as ProceedsResultsId','p.proceeds_end_time as ProceedsEndTime','p.proceeds_end as ProceedsEnd','pc.proceeds_results_id as ProceedsResultsId','pc.proceeds_results_title as ProceedsResultsTitle','c.case_id as CaseId','c.case_title as CaseTitle')
                                            ->join("yl_proceeds_results as pc",function($join){
                                                $join->on('p.proceeds_results_id','=','pc.proceeds_results_id');
                                            })
                                            ->join("yl_case as c",function($join){
                                                $join->on('p.case_id','=','c.case_id');
                                            });
                                            $count = $allMoney->count();
                                            $allMoney = $allMoney->offset($offset)->limit($pageSize)
                                                ->get()
                                                    ->map(function ($val){
                                                    $val = (array)$val;
                                                    return $val;
                                                    })
                                                    ->toArray();
                $sum_page = ceil($count / $pageSize);  //总页码
                // print_r($allMoney);die();
                 if ($page > $sum_page && $sum_page > 0) {
                     return RequestBackController::Structure(301);
                }else
                if($allMoney){
                    $YetPay = 0;
                    //合同收费金额总数
                    $CaseCount = DB::connection('mysql_other')->table('yl_case')->select('case_pay_count as CasePayCount');
                                                        $sum =$CaseCount->sum('case_pay_count');
                                                        $CaseCount =$CaseCount->get()
                                                                    ->map(function ($val){
                                                                    $val = (array)$val;
                                                                    return $val;
                                                                    })->toArray();
                                             
                    foreach ($allMoney as $k => $v) {
                            $allMoney[$k]['ProceedsCreatedTime'] =date('Y-m-d',$v['ProceedsCreatedTime']);
                            if($v['ProceedsEndTime'] == null || $v['ProceedsEndTime'] == ''){
                                $allMoney[$k]['ProceedsEndTime']= '--';
                            }else{
                                $allMoney[$k]['ProceedsEndTime'] =date('Y-m-d',$v['ProceedsEndTime']);
                            }
                           if($v['ProceedsEnd'] == 1){
                                $YetPay += $v['ProceedsCount'];
                            }
                    }
                    $row['SumMoney'] = $sum;
                    //已收合同金额
                    $row['YetPay'] = $YetPay;
                    //剩余应收金额
                    $RemainPay = $sum - $YetPay;
                    $row['RemainPay'] = $RemainPay;
					$row['list']= $allMoney;
					$row['count']=$count;
                    return RequestBackController::Structure(501,$row);
                }else{
                    return RequestBackController::Structure(504);
                }
            }
            if($UserPurview == 'Personal'){//显示个人
                $PersonMoney = DB::connection('mysql_other')->table('yl_proceeds as p')->select('p.proceeds_id as ProceedsId','p.proceeds_count as ProceedsCount','p.proceeds_creater_id as ProceedsCreaterId','p.proceeds_created_time as ProceedsCreatedTime','p.case_id as CaseId','p.proceeds_results_id as ProceedsResultsId','p.proceeds_end_time as ProceedsEndTime','p.proceeds_end as ProceedsEnd','pc.proceeds_results_id as ProceedsResultsId','pc.proceeds_results_title as ProceedsResultsTitle','c.case_id as CaseId','c.case_title as CaseTitle','c.case_member_id as CaseMemberId','c.case_from_id as CaseFromId','c.case_executor_id as CaseExecutorId','c.case_pay_count as CasePayCount')
                                            ->join("yl_proceeds_results as pc",function($join){
                                                $join->on('p.proceeds_results_id','=','pc.proceeds_results_id');
                                            })
                                            ->join("yl_case as c",function($join){
                                                $join->on('p.case_id','=','c.case_id');
                                            })
                                            // ->where('case_member_id','like','%'.$pos['UserId'].'%')->orWhere('case_from_id','like','%'.$pos['UserId'].'%')->orWhere('case_executor_id','like','%'.$pos['UserId'].'%')
                                            ->whereRaw("FIND_IN_SET($pos[UserId],case_member_id) or FIND_IN_SET($pos[UserId],case_from_id) or FIND_IN_SET($pos[UserId],case_executor_id)");
											$count = $PersonMoney->count();
                                            $PersonMoney = $PersonMoney->offset($offset)->limit($pageSize)
                                                
                                            ->get()
                                            ->map(function ($val){
                                            $val = (array)$val;
                                            return $val;
                                            })->toArray();
                 $sum_page = ceil($count / $pageSize);  //总页码
                // print_r($allMoney);die();
                 if ($page > $sum_page && $sum_page > 0) {
                     return RequestBackController::Structure(301);
                }else
                if($PersonMoney){
                    $YetPay = 0;
                    //合同收费金额总数
                    $CaseCount = DB::connection('mysql_other')->table('yl_case')->select('case_member_id as CaseMemberId','case_from_id as CaseFromId','case_executor_id as CaseExecutorId','case_pay_count as CasePayCount')
                                                        // ->where('case_member_id','like','%'.$pos['UserId'].'%')->orWhere('case_from_id','like','%'.$pos['UserId'].'%')->orWhere('case_executor_id','like','%'.$pos['UserId'].'%');
                                                        ->whereRaw("FIND_IN_SET($pos[UserId],case_member_id) or FIND_IN_SET($pos[UserId],case_from_id) or FIND_IN_SET($pos[UserId],case_executor_id)");
                                                        $sum =$CaseCount->sum('case_pay_count');
                                                        $CaseCount =$CaseCount->get()
                                                                    ->map(function ($val){
                                                                    $val = (array)$val;
                                                                    return $val;
                                                                    })->toArray();
                                                // print_r($sum);die();
                    foreach ($PersonMoney as $k => $v) {
                        $PersonMoney[$k]['ProceedsCreatedTime'] =date('Y-m-d',$v['ProceedsCreatedTime']);
                        if($v['ProceedsEndTime'] == null || $v['ProceedsEndTime'] == ''){
                            $PersonMoney[$k]['ProceedsEndTime']= '--';
                        }else{
                            $PersonMoney[$k]['ProceedsEndTime'] =date('Y-m-d',$v['ProceedsEndTime']);
                        }
                        if($v['ProceedsEnd'] == 1){
                                $YetPay += $v['ProceedsCount'];
                        }
                    }
                    $row['SumMoney'] = $sum;
                      //已收合同金额
                    $row['YetPay'] = $YetPay;
                     //剩余应收金额
                    $RemainPay = $sum - $YetPay;
                    $row['RemainPay'] = $RemainPay;
					$row['List'] = $PersonMoney;
					$row['count'] = $count;
                    return RequestBackController::Structure(501,$row);
                }else{
                    return RequestBackController::Structure(504);
                }
                    
            }
           if($UserPurview == 'None'){//暂无任何权限
            return RequestBackController::Structure(403);
            }
        }else{

            return RequestBackController::Structure(502);
        }
       
    }

    /*
    流水添加
    */
    public function AddTracking($request){
        if (!CheckController::CheckUser($request)) {
            return RequestBackController::Structure(205);
        }
        $pos = $request->all();
        if(!CheckController::CheckParameter($pos,['UserId','ProceedsCount','CaseId','ProceedsResultsId'])){//
           return RequestBackController::Structure(502);
       }else{
            $Purview = new PurviewController();
            $UserPurview = $Purview->PurviewVerdict($request,$pos['UserId']);
            //print_r($UserPurview);
            if($UserPurview == 'All'){//超级管理员
                $add = array();
                $add['proceeds_count'] = $pos['ProceedsCount'];
                $add['proceeds_creater_id'] = $pos['UserId'];
                $add['proceeds_results_id'] = $pos['ProceedsResultsId'];
                $add['proceeds_created_time'] = time();
                $add['case_id'] = $pos['CaseId'];
                $proceeds = DB::connection('mysql_other')->table('yl_proceeds')->insert($add);
                if($proceeds){
                    return RequestBackController::Structure(401);
                }else{
                    return RequestBackController::Structure(402);
                }
            }else{
                //无权限
                return RequestBackController::Structure(403);
            }
       }
        
    }
    /*
        收款状态
    */
    public function ProceedsResults($request){
        if (!CheckController::CheckUser($request)) {
            return RequestBackController::Structure(205);
        }
        $pos = $request->all();
        $Proceeds = DB::connection('mysql_other')->table('yl_proceeds_results')->select('proceeds_results_id as ProceedsResultsId',
		'proceeds_results_title as ProceedsResultsTitle')
                    ->get()->map(function ($val){
                        $val = (array)$val;
                        return $val;
                    })
                    ->toArray();
        if($Proceeds){
            return RequestBackController::Structure(501,$Proceeds);
        }else{
            return RequestBackController::Structure(504);
        }



    }
    /*
        资金流水编辑
    */
    public function EditTracking($request){
        if (!CheckController::CheckUser($request)) {
            return RequestBackController::Structure(205);
        }
        $pos = $request->all();
        if(!CheckController::CheckParameter($pos,['UserId','ProceedsId','ProceedsCount'])){//
           return RequestBackController::Structure(502);
        }else{
            $Purview = new PurviewController();
            $UserPurview = $Purview->PurviewVerdict($request,$pos['UserId']);
            if($UserPurview == 'All'){//超级管理员
                $map = array();
                $map['proceeds_count'] = $pos['ProceedsCount'];
                $proceeds = DB::connection('mysql_other')->table('yl_proceeds')->where('proceeds_id',$pos['ProceedsId'])->update($map);
                if($proceeds){
                    return RequestBackController::Structure(401);
                }else{
                    return RequestBackController::Structure(402);
                }
            }else{
                //无权限
                return RequestBackController::Structure(403);
            }
       }
        
    }
       /*
        确认到账
    */
    public function verify($request){
        if (!CheckController::CheckUser($request)) {
            return RequestBackController::Structure(205);
        }
        $pos = $request->all();
        if(!CheckController::CheckParameter($pos,['UserId','ProceedsId'])){//
           return RequestBackController::Structure(502);
        }else{
            $Purview = new PurviewController();
            $UserPurview = $Purview->PurviewVerdict($request,$pos['UserId']);
            if($UserPurview == 'All'){
                $ProceedsId = DB::connection('mysql_other')->table('yl_proceeds')->where('proceeds_id',$pos['ProceedsId'])->first();
                if($ProceedsId){
                    if($ProceedsId->proceeds_results_id ==1){//当收款信息已经为确认收款。不能点击确认收款按钮
                        return RequestBackController::Structure(602);
                    }else{
                        $map =array();
                        $map['proceeds_results_id']=1;//确认收款
                        $map['proceeds_end_time'] = time();
                        $map['proceeds_end'] = 1;//完成收款
                        $row = DB::connection('mysql_other')->table('yl_proceeds')->where('proceeds_id',$pos['ProceedsId'])->update($map);
                        if($row){
                            return RequestBackController::Structure(601);
                        }else{
                            return RequestBackController::Structure(602);
                        }
                    }

                }else{
                    return RequestBackController::Structure(505);
                }
            }else{
                //无权限
                return RequestBackController::Structure(403);
            }
            
        }

    }


    /*
    案件列表
    */
    public function CaseIn($request){
        if (!CheckController::CheckUser($request)) {
            return RequestBackController::Structure(205);
        }
        $pos = $request->all();
        $CaseInfo = DB::connection('mysql_other')->table('yl_case as c')->select('c.case_id as CaseId','c.case_title as CaseTitle')
                    ->get()->map(function ($val){
                        $val = (array)$val;
                        return $val;
                    })
                    ->toArray();
        if($CaseInfo){
            return RequestBackController::Structure(501,$CaseInfo);
        }else{
            return RequestBackController::Structure(504);
        }

    }
	/*
        收款状态
    */
    public function PayStatus($request){
        if (!CheckController::CheckUser($request)) {
            return RequestBackController::Structure(205);
        }
        $pos = $request->all();
        $Proceeds = DB::connection('mysql_other')->table('yl_proceeds_results')->select('proceeds_results_id as ProceedsResultsId','proceeds_results_title as ProceedsResultsTitle')
                    ->get()->map(function ($val){
                        $val = (array)$val;
                        return $val;
                    })
                    ->toArray();
        if($Proceeds){
            return RequestBackController::Structure(501,$Proceeds);
        }else{
            return RequestBackController::Structure(504);
        }
    }
	/*
      删除资金流水
    */
    public function DeleteTracking($request){
        if (!CheckController::CheckUser($request)) {
            return RequestBackController::Structure(205);
        }
        $pos = $request->all();
        if(!CheckController::CheckParameter($pos,['UserId','ProceedsId'])){//
           return RequestBackController::Structure(502);
        }else{
            $Purview = new PurviewController();
            $UserPurview = $Purview->PurviewVerdict($request,$pos['UserId']);
            if($UserPurview == 'All'){//超级管理员
                $proceeds = DB::connection('mysql_other')->table('yl_proceeds')->where('proceeds_id',$pos['ProceedsId'])->first();
				if($proceeds){
					if($proceeds->proceeds_end == 1){//完成收款不能删除
						return RequestBackController::Structure(608);
					}else{
						$row = DB::connection('mysql_other')->table('yl_proceeds')->where('proceeds_id',$pos['ProceedsId'])->delete();
						if($row){
							return RequestBackController::Structure(601);
						}else{
							return RequestBackController::Structure(602);
						}
					}
				}else{
					return RequestBackController::Structure(505);
				}
            }else{
                //无权限
                return RequestBackController::Structure(403);
            }
       }
        
    }








   
}
